package com.tedu._04weibo.controller;

import com.tedu._04weibo.base.response.JsonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/v1/file")
@Tag(name="图片上传")
public class UploadController {
    @PostMapping("/upload")
    @Operation(summary = "上传")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        filename = uuid+suffix;
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        String path = "d:/files";
        File file1 = new File(path + datePath);
        if (file1.exists()) {
            file1.mkdirs();
        }
        String filePath = path+ datePath+filename;
        file.transferTo(new File(filePath));
        return JsonResult.success(datePath+filename);
    }
}
